//
//  MYXbalanceViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/11.
//  Copyright © 2017年 hbjApple. All rights reserved.
//我的钱包界面

#import "MYXbalanceViewController.h"
#import "MYXAuthenticationViewController.h"

#import "CashWarningCell.h"
#import "MYXbalanceTopCell.h"
#import "MYXbalanceCell.h"
#import "MYXWithdrawalsViewController.h"
#import "MYXRechargeViewController.h"

static NSString *const MYXbalanceTopCellIdentifier = @"MYXbalanceTopCell";
static NSString *const MYXbalanceCellIdentifier = @"MYXbalanceCell";
@interface MYXbalanceViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>{
    MYXbalanceTopCell *MYXbalanceTopCe ;
    MYXbalanceCell *MYXbalanceCe;
    CGFloat lastOriginalY;
}
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;

@end

@implementation MYXbalanceViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    AdjustsScrollViewInsetNever(self, self.tableView);
    [self createUI];
}
#pragma mark - UI

- (void)createUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(-20);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.tableView registerNib:[UINib nibWithNibName:MYXbalanceTopCellIdentifier bundle:nil] forCellReuseIdentifier:MYXbalanceTopCellIdentifier];
     [self.tableView registerNib:[UINib nibWithNibName:MYXbalanceCellIdentifier bundle:nil] forCellReuseIdentifier:MYXbalanceCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:@"CashWarningCell" bundle:nil] forCellReuseIdentifier:cashWarningCellReuseId];
    
    
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -SafeAreaTopHeight, WIDTH, HEIGHT+20) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}
#pragma mark - request

- (void)loadNewData
{
    [self.tableView.mj_header beginRefreshing];
    [self.dataArr removeAllObjects];
    //[self mypublish:self.pageStart];
    
}

#pragma mark - tableview datasource and delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        return 3;
    }
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    if (indexPath.section==0) {
        MYXbalanceTopCe = [tableView dequeueReusableCellWithIdentifier:MYXbalanceTopCellIdentifier forIndexPath:indexPath];
        MYXbalanceTopCe.selectionStyle = UITableViewCellSelectionStyleNone;
        return MYXbalanceTopCe;
    }
 
    if (indexPath.row==0) {
        MYXbalanceCe = [tableView dequeueReusableCellWithIdentifier:MYXbalanceCellIdentifier forIndexPath:indexPath];
        MYXbalanceCe.selectionStyle = UITableViewCellSelectionStyleNone;
        [MYXbalanceCe.BalanceButton setTitle:@"提现" forState:UIControlStateNormal];
        WS(weakSelf);
        if ([[MYXMemberMdel sharedInstance].userinfo.user_money floatValue]> 0.9999) {
            [MYXbalanceCe.BalanceButton setEnabled:YES];
            MYXbalanceCe.BalanceB =^{
                MYXMemberMdel *model = [MYXMemberMdel sharedInstance] ;
                if ([model.is_idauth isEqualToString:@"1"]) {//实名认证通过
                    MYXWithdrawalsViewController *MYXWithdrawalsVC=[[MYXWithdrawalsViewController alloc]init];
                    MYXWithdrawalsVC.navigationItem.title = @"提现";
                    MYXWithdrawalsVC.restMoney = [MYXMemberMdel sharedInstance].userinfo.user_money;
                    [weakSelf.navigationController pushViewController:MYXWithdrawalsVC animated:YES];
                }else if ([model.is_idauth isEqualToString:@"0"]){//身份认证审核中
                    ShowSVProgressErrorMessage(@"身份认证审核中，通过后即可提现");
                  //  [SVProgressHUD showErrorWithStatus:@"身份认证审核中，通过后即可提现"];
                }else if ([model.is_idauth isEqualToString:@"-1"]){//审核被拒
                    ShowSVProgressErrorMessage(@"审核被拒，请重新提交资料");
                   // [SVProgressHUD showErrorWithStatus:@"审核被拒，请重新提交资料"];
                    MYXAuthenticationViewController *MYXAuthenticationVC =[[MYXAuthenticationViewController alloc]init];
                    MYXAuthenticationVC.MYXMemberMd = model;
                    MYXAuthenticationVC.navigationItem.title= @"实名认证";
                    [weakSelf.navigationController pushViewController:MYXAuthenticationVC animated:YES];
                }else{//还未认证或其他情况
                    ShowSVProgressErrorMessage(@"实名认证通过后才能提现");
                   // [SVProgressHUD showErrorWithStatus:@"实名认证通过后才能提现"];
                    MYXAuthenticationViewController *MYXAuthenticationVC =[[MYXAuthenticationViewController alloc]init];
                    MYXAuthenticationVC.MYXMemberMd = model;
                    MYXAuthenticationVC.navigationItem.title= @"实名认证";
                    [weakSelf.navigationController pushViewController:MYXAuthenticationVC animated:YES];
                }

            };
        }else{
            [MYXbalanceCe.BalanceButton setEnabled:NO];
            [MYXbalanceCe.BalanceButton setTitleColor:MYYColorFromRGB(0xa6a6a6) forState:UIControlStateNormal];
            MYXbalanceCe.BalanceButton.backgroundColor =MYYColorFromRGB(0xececec);
            
        }
        return MYXbalanceCe;
      
    }else if (indexPath.row==1){
        MYXbalanceCe = [tableView dequeueReusableCellWithIdentifier:MYXbalanceCellIdentifier forIndexPath:indexPath];
        MYXbalanceCe.selectionStyle = UITableViewCellSelectionStyleNone;
        MYXbalanceCe.BalanceB =^{
            MYXRechargeViewController *MYXRechargeVC =[[MYXRechargeViewController alloc]init];
            MYXRechargeVC.navigationItem.title = @"充值";
            [weakSelf.navigationController pushViewController:MYXRechargeVC animated:YES];
           
        };
        return MYXbalanceCe;
    }else{
    
        CashWarningCell *warningCell = [CashWarningCell loadNibWithCell];
        
        return warningCell;
    }
   // return MYXbalanceCe;
   
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 272+68;
    }
   
    return 55;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==1&&indexPath.row==0) {
        if ([[MYXMemberMdel sharedInstance].userinfo.user_money floatValue]> 0.9999) {
            MYXWithdrawalsViewController *MYXWithdrawalsVC=[[MYXWithdrawalsViewController alloc]init];
            MYXWithdrawalsVC.navigationItem.title = @"提现";
            MYXWithdrawalsVC.restMoney = [MYXMemberMdel sharedInstance].userinfo.user_money;
            [self.navigationController pushViewController:MYXWithdrawalsVC animated:YES];
        }else{
            [self showFailureTopMessage:@"当您的金额超过1元之后可进行提现！"];
          //  [JXTHUDHelper showHUDText:@"当您的金额超过1元之后可进行提现！" type:@"warning" inView:nil];

        }
        
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"scrollview offset %f",scrollView.contentOffset.y);
    lastOriginalY = scrollView.contentOffset.y;
    CGFloat consTop;
    if (lastOriginalY < -20) {
        consTop  = - 20 - lastOriginalY;
    }else{
        consTop = 0;
    }
    
    CGFloat alpha = (consTop+360)/360;
    alpha=fabs(alpha);
    alpha=fabs(1-alpha);
    
    alpha=alpha<0.2? 0:alpha-0.2;
    if (MYXbalanceTopCe) {
       MYXbalanceTopCe.bg_Topcons.constant = -consTop;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
